<?php
	$labels = array();
	$func	= array('view', 'delete', 'edit');
	$query = 'select * from '.$table.' order by id desc';
	$link = '';

	/*$table_header = "<ul class=\"table-header\">\n<li class=\"icon\"><!-- --></li>\n<li class=\"num-items\">items per page ";
	$table_header .= "<select><option>10</option><option>20</option><option>30</option></select>\n";
	$table_header .= "</li>\n</ul>\n";*/
	$table_header = '';

	switch($table) {

		// user specific
		
		case 'dashboard':
			echo 'Dashboard.';
			break;

		case 'subscription':
			$query = 'select subscriptions.*, subscription_types.label from subscriptions inner join subscription_types on subscriptions.subscriptiontypeid = subscription_types.id where userid = '.$user_id.' and paid=1 order by id desc';
			$result = $cn->Data($query, false);
			$percentage = 100;
			if (sizeof($result) > 0) {
				$now = time();
				$start = strtotime($result[0]['startdate']);
				$end = strtotime($result[0]['enddate']);
				if ($now > $end) $now = $end;
				if ($end == $start) $percentage = 100;
				else $percentage = round(
								ceil(abs($now - $start) / 86400) /
								ceil(abs($end - $start) / 86400)
							  * 100 );
				if ($percentage == 100) {
					echo '<span class="dark"><h3>Current subscription: <strong>'.$result[0]['label'].". EXPIRED.</strong></h3>\n";
					echo "<ul class=\"progress\"><li data-percentage=\"$percentage\"><!-- --></li></ul>\n";
					echo "<ul class=\"details\"><li class=\"label\">Start Date:</li><li>".date('D, j M Y', strtotime($result[0]['startdate']))."</li><li class=\"label\">End Date:</li><li>".date('D, j M Y', strtotime($result[0]['enddate']))."</li></ul></span>\n";
				} else {
					echo '<span class="dark"><h3>Current subscription: <strong>'.$result[0]['label']."</strong></h3>\n";
					echo "<ul class=\"progress\"><li data-percentage=\"$percentage\"><!-- --></li></ul>\n";
					echo "<ul class=\"details\"><li class=\"label\">Start Date:</li><li>".date('D, j M Y', strtotime($result[0]['startdate']))."</li><li class=\"label\">End Date:</li><li>".date('D, j M Y', strtotime($result[0]['enddate']))."</li></ul></span>\n";
					echo "<input type=\"hidden\" value=\"".$result[0]['subscriptiontypeid']."\" id=\"subscription-id\" />\n";
				}
				
				echo "<h4 class=\"top-space\">Subscription options available:</h4>\n";
			} else {
				echo "<h3>You do not have an active subscription.</h3>\n";
				echo "<h4>Please select one of the options available:</h4>\n";
			}
			$labels = array('id', 'Title', /*'Duration (days)',*/ 'Free Days', 'Daily Rate', 'Price');
			$func = array('subscribe');
			/*if ($percentage == 100) */
			echo $cn->OutputTable('select id, label, freedays, dailyrate, price from subscription_types order by id desc', $labels, $func, 'table select-table', false);

			break;

		case 'settcredit':
			$credit = $cn->Data('select * from settcredit where userid='.$user_id, false);
			echo "<span class=\"dark\">\n";
			echo "<h3>Sett Credit Balance: <strong>".$credit[0]['settcredit']."</strong> days available</h3>\n";
			$res = $cn->Data('select subscriptiontypeid from subscriptions where userid='.$user_id, false);
			$subid = $res[0]['subscriptiontypeid'];
			$res = $cn->Data('select * from subscription_types where id='.$subid, false);
			$dailyrate = $res[0]['dailyrate'];

			$res = $cn->Data('select startdate, enddate from subscriptions where userid='.$user_id.' and paid=1', false);
			$now = time();
			$start = strtotime($res[0]['startdate']);
			$end = strtotime($res[0]['enddate']);
			if ($now > $end) $now = $end;
			if ($end == $start) $percentage = 100;
			else $percentage = round(ceil(abs($now - $start) / 86400) / ceil(abs($end - $start) / 86400) * 100);

			if ($dailyrate > 0 && $percentage < 100)
				echo "<a class=\"add_new\" href=\"".$path.$table."/add\"><img src=\"img/blank.gif\" alt=\"\" /> Purchase More Credit</a>";
			if ($percentage == 100) echo "<h5>NOTE: Your subscription has expired.</h5>\n";
			echo "</span>";
			unset($subid, $res, $dailyrate, $now, $start, $end, $percentage);
			echo "<h3>Free days available: <strong>".$credit[0]['freedays']."</strong> days available</h3>\n";
			echo $cn->OutputTable("select date, description from logs where userid=$user_id and type='visit' order by date desc", array('Recent Visits', 'Details'), array(), 'table list-table', false);
			break;

		case 'bookings':
			echo "<div id=\"full-calendar\"></div>\n";
			//$query = 'select id, date, userid, label, amount, content, paid from '.$table.' order by date desc';
			//$labels = array('id', 'Date', 'User', 'Invoice Number', 'Amount', 'Details', 'Paid');
			//$func	= array('view');
			//echo $table_header;
			//echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'training_bookings':
			$func = array('delete');
			$query = 'select training_bookings.id, training.label, training_bookings.date, training_bookings.delegates, training_bookings.additional, training_bookings.paid from training_bookings inner join training on training_bookings.trainingid = training.id where userid='.$user_id;
			$labels = array('id', 'Training', 'Date', 'Delegates', 'Additional Requests', 'Paid');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			//echo $query;
			break;

		// company specific

		case 'company_users':
			$query = 'select id, name, mob, email, accesscode from '.$table.' where companyid = '.$user_id;
			$labels = array('id', 'Name', 'Mobile Number', 'Email', 'Access Code');
			$func	= array('delete', 'edit');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		// admin specific

		case 'subscription_types':
			$query = 'select id, label, freedays, dailyrate, price from '.$table.' order by id desc';
			$labels = array('id', 'Title', /*'Duration (days)',*/ 'Free Days', 'Daily Rate', 'Price');
			//$func	= array('delete', 'edit');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'invoices':
			$query = 'select id, date, userid, label, amount, content, paid from '.$table.' order by date desc';
			$labels = array('id', 'Date', 'User', 'Invoice Number', 'Amount', 'Details', 'Paid');
			$func	= array('view');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'users':
			$query = 'select id, title, name, position, company, mob, email, province, verified, accesscode from '.$table.' where iscompany = 0 order by id desc';
			$labels = array('id', 'Title', 'Name', 'Position', 'Company', 'Mobile', 'Email', 'Province', 'Verified', 'Access Code');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'companies':
			$query = 'select id, company, name, mob, email, province, verified, accesscode from users where iscompany = 1 order by id desc';
			$labels = array('id', 'Company Name', 'Account Manager Name', 'Account Manager Mobile', 'Account Manager Email', 'Province', 'Verified', 'Access Code');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'buildings':
			$labels = array('id', 'Building Title', 'Manager', 'Telephone Number', 'Mobile Number', 'Email', 'Address', 'Province');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'services':
			$labels = array('id', 'Title', 'Description', 'Cost', 'Cost is per person');
			$func	= array('delete', 'edit');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'logs':
			$func = array();
			$labels = array('id', 'User', 'Date', 'Description', 'Type');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'alerts':
			$func = array('view');
			$query = 'select alerts.id, alerts.date, logs.description, logs.userid from alerts inner join logs on alerts.logid = logs.id where viewed=0';
			$labels = array('id', 'Date', 'Description', 'User');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		case 'training':
			$query = 'select id, label, starttime, endtime, mincapacity, maxcapacity, price from '.$table;		
			$labels = array('id', 'Label', 'Start Time', 'End Time', 'Min Capacity', 'Max Capacity', 'Price');
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);
			break;

		default:
			$query = 'select * from '.$table.' order by id desc';
			echo $table_header;
			echo $cn->OutputTable($query, $labels, $func, 'table list-table', false);

		
	}

	//if ($query != '') 

?>